<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-if="dsInfo && dsInfo.searchExp != '0'" label-width="68px">
      <el-form-item :label="dsInfo.searchLabel" prop="searchValue" >
        <el-date-picker style="width: 180px" v-if="dsInfo.searchDs == 'date'"
          v-model="queryParams.searchValue"
          value-format="YYYY-MM-DD"
          type="date"
        ></el-date-picker>
        <el-date-picker style="width: 280px" v-else-if="dsInfo.searchDs == 'daterange'"
          v-model="dateranges"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
        <el-select v-model="queryParams.searchValue" placeholder="请选择类别" style="width: 160px" clearable v-else-if="dsInfo.searchDs">
          <el-option
            v-for="dict in dev_dataset_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        <el-input v-else
          v-model="queryParams.searchValue"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row v-if="dsInfo && dsInfo.type == 'sum'">
      <el-col :sm="16" :lg="12" :offset="6" >
        <el-result
          icon="success"
          title="计算结果"
          :sub-title="dataList"
        >
        </el-result>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="dataList" v-else>
      <el-table-column type="index" width="55" align="center" />
      <el-table-column :label="c.title" align="center" :prop="c.field" v-for="(c,i) in columns" :key="i"/>
    </el-table>
    
  </div>
</template>

<script setup name="Dataset">
import { getDatasetInfo, queryDs } from "@/api/dev/dataset";

const { proxy } = getCurrentInstance();
const route = useRoute();

const dataList = ref([]);
const loading = ref(true);
const total = ref(0);
const dsCode = ref(null);
const dsInfo = ref(null);
const dateranges = ref([]);
const columns = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    searchValue: null
  }
});

const { queryParams, form } = toRefs(data);

/** 查询数据集列表 */
function getList() {
  loading.value = true;
  if(dsInfo.value.searchDs == 'daterange' && dateranges){    
    queryParams.value.searchValue = dateranges.value[0]+','+dateranges.value[1];
  }
  queryDs({code:dsCode.value, param:queryParams.value.searchValue}).then(response => {
    
    if(response.data != null && response.data.length > 0){
        var d = response.data[0];
        var cols = [];
        for(var k in d){
            cols.push({field:k,title:k});
        }
        cols.sort();
        columns.value = cols;
    }

    dataList.value = response.data;
    total.value = response.total;
    loading.value = false;
  });
}


/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateranges.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

(() => {
  dsCode.value = route.params && route.params.code;
  if (dsCode.value) {
    getDatasetInfo(dsCode.value).then(response=>{
      dsInfo.value = response.data;
      getList();
    })
  }
})();

</script>
